
package com.zhu.salesmanagement.overlays;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

/**
 * 地图覆盖物
 */
public class StadiumOverlay extends ItemizedOverlay<StadiumOverlayItem> {
    private List<StadiumOverlayItem> items = new ArrayList<StadiumOverlayItem>();
    private Drawable marker = null;
    private Context context;
    private OnTapClickListener mOnTapClickListener = null;

    public StadiumOverlay(Drawable defaultMaker, Context ctx, List<StadiumOverlayItem> items, OnTapClickListener listener) {
        super(defaultMaker);
        this.marker = defaultMaker;
        if (items != null) {
            this.items = items;
            initOverlayItems();
        }
        this.context = ctx;
        mOnTapClickListener = listener;
    }

    public void addOverlay(StadiumOverlayItem overlay) {
        items.add(overlay);
        populate();
    }

    protected boolean isRouteDisplayed() {
        return false;
    }

    @Override
    protected StadiumOverlayItem createItem(int i) {
        return items.get(i);
    }

    @Override
    public int size() {
        return items.size();
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);
        boundCenterBottom(marker);
    }

    private void initOverlayItems() {
        if (this.items != null) {
            for (StadiumOverlayItem item : items) {
                if (item != null) {
                    this.addOverlay(item);
                }
            }
        }
    }

    @Override
    public boolean onTap(GeoPoint p, MapView mapView) {
        return super.onTap(p, mapView);
    }

    @Override
    protected boolean onTap(int i) {
        setFocus(items.get(i));
        if(mOnTapClickListener != null){
            mOnTapClickListener.tapPoint(items.get(i));
        }
// 
//        GeoPoint pt = mGeoList.get(i).getPoint();
//        ItemizedOverlayDemo.mMapView.updateViewLayout( ItemizedOverlayDemo.mPopView,
//                new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
//                        pt, MapView.LayoutParams.BOTTOM_CENTER));
//        ItemizedOverlayDemo.mPopView.setVisibility(View.VISIBLE);
//        Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),
//                Toast.LENGTH_SHORT).show();
        return true;
    }

    public interface OnTapClickListener{
        public void tapPoint(StadiumOverlayItem item);
    }
}
